<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../../styles/common.css" />
    <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
<div class="addBrand-container" id="member-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :rules="rules"
                :inline="false"
                label-width="180px"
                class="demo-ruleForm"
        >
            <el-form-item label="账号:" prop="username">
                <el-input v-model="ruleForm.username" placeholder="请输入账号" maxlength="20"/>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
                <el-input show-password   v-model="ruleForm.password" placeholder="请输入密码" maxlength="20" />
            </el-form-item>
            <el-form-item label="管理员昵称:" prop="nickname">
                <el-input v-model="ruleForm.nickname" placeholder="请输入管理员昵称" maxlength="20"/>
            </el-form-item>
            <el-form-item label="管理员真实姓名:" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入管理员真实姓名" maxlength="20"/>
            </el-form-item>
            <el-form-item label="管理员年龄:" prop="age">
                <el-input v-model="ruleForm.age" placeholder="请输入管理员年龄" maxlength="20"/>
            </el-form-item>
            <el-form-item label="管理员生日:" prop="birthday">
                <el-date-picker
                        v-model="ruleForm.birthday"
                        type="date"
                        placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="类型">
                <el-select v-model="ruleForm.type" prop="status">
                    <el-option label="超级管理员" value="超级管理员"></el-option>
                    <el-option label="管理员" value="管理员"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="手机号:" prop="phone" >
                <el-input v-model="ruleForm.phone" placeholder="请输入手机号" maxlength="20"/>
            </el-form-item>
            <el-form-item label="邮箱:" prop="email">
                <el-input v-model="ruleForm.email" placeholder="请输入邮箱" maxlength="20"/>
            </el-form-item>
            <el-form-item label="身份证号:" prop="idNumber">
                <el-input v-model="ruleForm.idNumber" placeholder="请输入身份证号" maxlength="20"/>
            </el-form-item>

            <el-form-item label="头像:" prop="region" class="uploadImg">
                <el-upload
                			  class="avatar-uploader"
                			  action="/apis/common/upload"
                			  :show-file-list="false"
                			  :on-success="handleAvatarSuccess"
                			  :before-upload="beforeAvatarUpload">
                			  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                			  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                			</el-upload>
            </el-form-item>

            <div class="subBox address">
                <el-form-item>
                    <el-button  @click="goBack()">
                        取消
                    </el-button>
                    <el-button
                            type="primary"
                            @click="submitForm('ruleForm', false)"
                    >
                        保存
                    </el-button>
                    <el-button
                            v-if="actionType == 'add'"
                            type="primary"
                            class="continue"
                            @click="submitForm('ruleForm', true)"
                    >
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/admin.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
    new Vue({
        el: '#member-add-app',
        data() {
            return {
                id: '',
				imageUrl:'',
                actionType : '',
                ruleForm : {
                    'adminId': '',
                    'userName': '',
                    'password': '',
                    'name': '',
                    'nickname': '',
                    'age': '',
                    'birthday': '',
                    'type': '',
                    'email': '',
                    'phone': '',
                    'image': '',
                    'idNumber': '',
                }
            }
        },
        computed: {
            rules () {
                return {
                    //账号
                    'username': [
                        {
                            required: true, 'validator': checkUserName, trigger: 'blur'
                        }
                    ],
                    //姓名
                    'name': [{ required: true, 'validator': checkName, 'trigger': 'blur' }],
                    'phone': [{ 'required': true, 'validator': checkPhone, 'trigger': 'blur' }],
                    'idNumber': [{ 'required': true, 'validator': validID, 'trigger': 'blur' }],
                    'password': [{ 'required': true, 'trigger': 'blur' }]

                }
            }
        },
        created() {
            /*获取参数id*/
            this.id = requestUrlParam('id');//从网址上解析参数
            /*判断参数是id还是add  -》如果是id调用edit   否则调用add*/
            this.actionType = this.id ? 'edit' : 'add';
            if (this.id) {
                this.init()
            }
        },
        mounted() {
        },
        methods: {
			handleAvatarSuccess(res, file) {
							console.log(res.data);
							this.ruleForm.image=res.data
							console.log(this.ruleForm.image);
							console.log(file);
						        this.imageUrl = URL.createObjectURL(file.raw);
						      },
            async init () {
                //发起一个异步请求，获取用户信息
                queryAdminById(this.id).then(res => {
                    // console.log(res);
                    if (String(res.code) === '1') {
                        // console.log(res.data);
                        this.ruleForm = res.data;
						this.imageUrl = $minioUrl+res.data.image
                        // this.ruleForm.password = ''
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            submitForm (formName, st) {
                var _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            //params = 数据
                            const params = {
                                ...this.ruleForm,
                                sex: this.ruleForm.sex === '女' ? '0' : '1'
                            };
                            /*找到发送请求的方法*/
                            addAdmin(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('员工添加成功！');
                                    if (!st) {
                                        this.goBack()
                                    } else {
                                        this.ruleForm = {
                                            'adminId': '',
                                            'userName': '',
                                            'password': '',
                                            'name': '',
                                            'nickname': '',
                                            'age': '',
                                            'birthday': '',
                                            'type': '',
                                            'email': '',
                                            'phone': '',
                                            'image': '',
                                            'idNumber': '',
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            const params = {
                                ...this.ruleForm
                            };
                            editAdmin(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('员工信息修改成功！');
                                    setTimeout(function (){
                                        _this.goBack()
                                    },1000);

                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false
                    }
                })
            },
            goBack(){
                window.parent.menuHandle({
                    id: '11',
                    url: 'page/admin/list.html',
                    name: '管理员管理'
                },false)
            }
        }
    })
</script>
</body>
</html>